<?php
/**
 * 可逆的加解密，除非知道密钥，否则很难破译
 * @link http://www.nowamagic.net/librarys/veda/detail/1947
 */
class crypt {
	
	function encode( $txt, $key )
	{
		$txt = $txt;
		$iv = substr( md5( $key ), 0, mcrypt_get_iv_size( MCRYPT_CAST_256, MCRYPT_MODE_CFB ) );
		$code = mcrypt_cfb( MCRYPT_CAST_256, $key, $txt, MCRYPT_ENCRYPT, $iv );
		return base64_encode( $code );
	}
	
	function decode( $code, $key )
	{
		$code = base64_decode( $code );
		$iv = substr( md5( $key ), 0, mcrypt_get_iv_size( MCRYPT_CAST_256, MCRYPT_MODE_CFB ) );
		$txt = mcrypt_cfb( MCRYPT_CAST_256, $key, $code, MCRYPT_DECRYPT, $iv );
		return $txt;
	}
}
